package com.ef.web.action;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import com.ef.constant.ELeadsDealType;
import com.ef.domain.Company;
import com.ef.domain.CorpDealHistory;
import com.ef.domain.School;
import com.ef.domain.User;
import com.ef.service.CorpDealHistoryService;
import com.ef.service.UserService;
import com.ef.util.CustomExample;

public class CorpCCAssignAction extends CompanyAction {

	private static final long serialVersionUID = 1L;

	private Map<String, String> ccs;

	private boolean noCc = true;

	protected UserService userService;

	protected CorpDealHistoryService corpDealHistoryService;

	public String assignToCc() {
		String ids = getRequest().getParameter("ids");
		String[] id = ids.split(",");
		String cc = getRequest().getParameter("cc_id");
		User user = userService.findById(Long.valueOf(cc));

		for (String i : id) {
			Company l = companyService.findById(Long.valueOf(i));
			//
			CorpDealHistory dh = new CorpDealHistory();
			dh.setCurState(user.getName());
			if (l.getCc() != null) {
				dh.setPreState(l.getCc().getName());
			}
			dh.setDealTime(new Date());
			dh.setOperUser(this.getSessionUser());
			dh.setType(ELeadsDealType.AssignedToCC);
			dh.setCompany(l);
			corpDealHistoryService.saveOrUpdate(dh);
			//
			l.setCc(user);
			l.setCcAssignTime(new Date());
			companyService.saveOrUpdate(l);
		}
		fetchListData();
		return LIST;
	}

	@SuppressWarnings("serial")
	public String list() {
		fetchListData();
		this.listResult = companyService.findByExample(
				new CustomExample<Company>(this.getModel()) {
					public void appendCondition(Criteria criteria) {
						if (noCc) {
							criteria.add(Restrictions.isNull("cc"));
						}
						this.enableLike(MatchMode.ANYWHERE);
					}
				}, this.getStartIndex(), this.getPageSize());
		return LIST;
	}

	protected void fetchListData() {
		Map<String, String> map = new LinkedHashMap<String, String>();
		School school = getSessionUser().getSchool();
		List<User> users = null;
		if (school == null)
			users = userService.findAllCc();
		else {
			users = userService.findAllCcSameSchool(school);
		}

		for (User u : users) {
			map.put(u.getId().toString(), u.getName());
		}
		ccs = map;
	}

	public Map<String, String> getCcs() {
		return ccs;
	}

	public void setCcs(Map<String, String> ccs) {
		this.ccs = ccs;
	}

	public boolean isNoCc() {
		return noCc;
	}

	public void setNoCc(boolean noCc) {
		this.noCc = noCc;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public CorpDealHistoryService getCorpDealHistoryService() {
		return corpDealHistoryService;
	}

	public void setCorpDealHistoryService(
			CorpDealHistoryService corpDealHistoryService) {
		this.corpDealHistoryService = corpDealHistoryService;
	}

}
